package com.nsoft.server.service.daoImpl;

import java.util.List;

import javax.persistence.Query;

import com.nsoft.server.master.beans.PartyMaster;
import com.nsoft.server.service.dao.PartyDao;
import com.nsoft.thickclient.trackMove.intr.party.Party;
import com.nsoft.thickclient.trackMove.intr.party.ThinParty;
import com.nsoft.thickclient.trackMove.intr.util.StringHelper;

public class PartyDaoImpl extends
		AbstractMasterGenericJPADAO<PartyMaster, Integer> implements PartyDao {

	public List<ThinParty> getAllThinParties(ThinParty thinParty) {

		StringBuffer sb = new StringBuffer();
		sb
				.append("select New com.nsoft.thickclient.trackMove.intr.party.ThinParty"
						+ "(master.partyCharName,master.partyNumId) " +
								"from com.nsoft.server.master.beans.PartyMaster"
						+ " master where 1=1 ");
		if (thinParty.getName() != null
				&& !thinParty.getName().equalsIgnoreCase("")
				&& !thinParty.getName().equalsIgnoreCase(" ")) {
			String name =StringHelper.replace("*", "%", thinParty.getName());
			sb.append("and  master.partyCharName like '" + name + "'");
		}
		if (thinParty.getId() != null) {
			sb.append("and  master.partyNumId ="+thinParty.getId().intValue());
		}
		Query query = getEntityManager().createQuery(sb.toString());
		return query.getResultList();
	}

	public List<PartyMaster> searchParties(Party party) {

		String name = "";
		StringBuffer sb = new StringBuffer();
		sb
				.append("select master "
						+ " from com.nsoft.server.master.beans.PartyMaster"
						+ " master where 1=1 ");
		if (party.getName() != null
				&& !party.getName().equalsIgnoreCase("")
				&& !party.getName().equalsIgnoreCase(" ")) {
			name =StringHelper.replace("*", "%", party.getName());
			sb.append("and  master.partyCharName like '" + name + "'");
		}

		if (party.getPan() != null
				&& !party.getPan().equalsIgnoreCase("")
				&& !party.getPan().equalsIgnoreCase(" ")) {
			name =StringHelper.replace("*", "%", party.getPan());
			sb.append("and  master.partyCharPan like '" + name + "'");
		}

		if (party.getTan() != null
				&& !party.getTan().equalsIgnoreCase("")
				&& !party.getTan().equalsIgnoreCase(" ")) {
			name =StringHelper.replace("*", "%", party.getTan());
			sb.append("and  master.partyCharTan like '" + name + "'");
		}

		if (party.getTin() != null
				&& !party.getTin().equalsIgnoreCase("")
				&& !party.getTin().equalsIgnoreCase(" ")) {
			name =StringHelper.replace("*", "%", party.getTin());
			sb.append("and  master.partyCharTin like '" + name + "'");
		}

		if (party.getExim() != null
				&& !party.getExim().equalsIgnoreCase("")
				&& !party.getExim().equalsIgnoreCase(" ")) {
			name =StringHelper.replace("*", "%", party.getExim());
			sb.append("and  master.partyCharExim like '" + name + "'");
		}

		Query query = getEntityManager().createQuery(sb.toString());
		return query.getResultList();
	}

}
